package com.zmm.merchant.controller;

import com.zmm.common.base.model.Result;
import com.zmm.common.base.model.ResultCode;
import com.zmm.common.exception.handle.BusinessException;
import com.zmm.common.valid.AddGroup;
import com.zmm.common.valid.UpdateGroup;
import com.zmm.merchant.model.Merchant;
import com.zmm.merchant.service.MerchantService;
import com.zmm.util.verify.BindingResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author 900045
 * @description:
 * @name MerchantController
 * @date By 2021-07-06 11:23:17
 */
@Controller
@Slf4j
@RequestMapping("/merchant")
public class MerchantController {
	
	@Resource
	private MerchantService merchantService;


	/**
	 * 去到这个 注册页面
	 * @author: 900045
	 * @date: 2021-07-06 11:52:16
	 * @throws 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/to/register",method = RequestMethod.GET)
	public String toRegisterMerchant(){
		return "merchant_register";
	}

	/**
	 * 新增商家 - 控制层
	 * @author: 900045
	 * @date: 2021-07-06 11:49:33
	 * @throws 
	 * @param merchant: 商家实体
	 * @param result: 校验参数结果   
	 * @return: com.zmm.common.base.model.Result
	 **/
	@ResponseBody
	@RequestMapping(value = "/do/add",method = RequestMethod.POST)
	public Result addMerchant(@Validated({AddGroup.class}) Merchant merchant, BindingResult result){

		// 1.表达校验
		if (result.hasErrors()){
			//  Model model
			//model.addAttribute("orders",result)
			return BindingResultUtil.paramVerify(result);
		}
		// 2.包裹业务
		try {
			// 2.1 执行业务
			boolean flag = merchantService.addMerchant(merchant);
			if (!flag){
				// 新增失败
				return Result.error(ResultCode.INSERT_FAILED);
			}
		} catch (BusinessException e){
			log.error("SAVE-MERCHANT-[addMerchant]  end: e-{}", e);
			return Result.error(e.getCode().getCode(),e.getMessage());
		} catch (Exception e){
			log.error("SAVE-MERCHANT  end: e-{}", e);
		}
		// 最终返回
		return Result.ok();
	}

	/**
	 * 去到这个 更新页面
	 * @author: 900045
	 * @date: 2021-07-06 15:26:16
	 * @throws 
	 * @param model: 
	 * @param MerchantId: 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/to/update",method = RequestMethod.GET)
	public String toUpdateMerchant(Model model, Integer MerchantId){
		Merchant merchant = merchantService.findOneMerchantByMerchantId(MerchantId);
		model.addAttribute("merchant",merchant);
		return "merchant_update";
	}


	/**
	 * 更新商家信息 - 控制层
	 * @author: 900045
	 * @date: 2021-07-06 15:26:29
	 * @throws 
	 * @param merchant: 
	 * @param result: 
	 * @return: com.zmm.common.base.model.Result
	 **/
	@ResponseBody
	@RequestMapping(value = "/do/update",method = RequestMethod.POST)
	public Result updateMerchant(@Validated({UpdateGroup.class}) Merchant merchant, BindingResult result){
		
		// 1.表达校验
		if (result.hasErrors()){
			//  Model model
			//model.addAttribute("orders",result)
			return BindingResultUtil.paramVerify(result);
		}
		// 2.包裹业务
		try {
			boolean flag = merchantService.modifyMerchant(merchant);
			if (!flag){
				// 更新失败
				return Result.error(ResultCode.UPDATE_FAILED);
			}
		} catch (Exception e){
			log.error("UPDATE-MERCHANT  end: e-{}", e);
		}
		return Result.ok();
	}

	/**
	 * 根据 商家ID 查看详情 -- 控制层
	 * @author: 900045
	 * @date: 2021-07-06 15:53:12
	 * @throws 
	 * @param model: 
	 * @param merchantId: 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/find/merchantId",method = RequestMethod.GET)
	public String findMerchantByMerchantId(Model model, Integer merchantId){
		Merchant merchant = merchantService.findOneMerchantByMerchantId(merchantId);
		model.addAttribute("merchant",merchant);
		return "merchant_view";
	}


	/**
	 * 去到这个 更审核页面
	 * @author: 900045
	 * @date: 2021-07-06 15:56:32
	 * @throws 
	 * @param model: 
	 * @param merchantId: 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/to/update/status",method = RequestMethod.GET)
	public String toUpdateMerchantStatus(Model model, Integer merchantId){
		Merchant merchant = merchantService.findOneMerchantByMerchantId(merchantId);
		model.addAttribute("merchant",merchant);
		return "merchant_audit";
	}

	/**
	 * 审核商家 -- 控制层
	 * @author: 900045
	 * @date: 2021-07-06 15:58:08
	 * @throws 
	 * @param status: 
	 * @param merchantId: 
	 * @return: void
	 **/
	@ResponseBody
	@RequestMapping(value = "/do/update/status",method = RequestMethod.POST)
	public Result updateMerchantStatus(Integer status,Integer merchantId){
		try {
			boolean flag = merchantService.modifyMerchantStatus(status,merchantId);
			if (!flag){
				// 更新失败
				return Result.error(ResultCode.UPDATE_FAILED);
			}
		} catch (Exception e){
			log.error("UPDATE-MERCHANT-[updateMerchantStatus]  end: e-{}", e);
		}
		return Result.ok();
	}

	/**
	 * 去到这个 商家上下架页面
	 * @author: 900045
	 * @date: 2021-07-06 16:09:42
	 * @throws 
	 * @param model: 
	 * @param merchantId: 
	 * @return: java.lang.String
	 **/
	@RequestMapping(value = "/to/update/sold_out",method = RequestMethod.GET)
	public String toUpdateSoldOut(Model model ,Integer merchantId){
		Merchant merchant = merchantService.findOneMerchantByMerchantId(merchantId);
		model.addAttribute("merchant",merchant);
		return "merchant_sold_out";
	}



	/**
	 * 上下架商家 -- 控制层
	 * @author: 900045
	 * @date: 2021-07-06 16:12:54
	 * @throws 
	 * @param soldOut: 
	 * @param merchantId: 
	 * @return: void
	 **/
	@ResponseBody
	@RequestMapping(value = "/do/update/sold_out",method = RequestMethod.POST)
	public Result doUpdatesSoldOut(Integer soldOut,Integer merchantId){
		try {
			boolean flag = merchantService.modifyMerchantSoldOut(soldOut,merchantId);
			if (!flag){
				// 更新失败
				return Result.error(ResultCode.UPDATE_FAILED);
			}
		} catch (BusinessException e) {
			log.error("UPDATE-MERCHANT-[doUpdatesSoldOut]  end: e-{}", e);
			return Result.error(e.getCode().getCode(),e.getMessage());
		}
		return Result.ok();
	}

}
